package com.learn.grpc.loadbalance;

import io.grpc.NameResolver;
import io.grpc.NameResolverProvider;
import lombok.extern.slf4j.Slf4j;

import java.net.URI;

// 默认有: DnsNameResolverProvider 和 UdsNameResolverProvider
@Slf4j
public class LbNameProvider extends NameResolverProvider {

    @Override
    protected boolean isAvailable() {
        return true;
    }

    @Override
    protected int priority() {
        return 0;
    }

    @Override
    public NameResolver newNameResolver(URI uri, NameResolver.Args args) {
        return new LbNameResolver(uri.getHost());
    }

    @Override
    public String getDefaultScheme() {
        // 协议名称, 服务端名称为 lb://xxx 之类
        return "lb";
    }
}
